import 'package:flutter/cupertino.dart';
import 'package:hitutor/Util/Global.dart';
import 'package:hitutor/Util/HttpGo.dart';
import 'package:hitutor/Util/MyColor.dart';
import 'package:hitutor/Util/PageUtil.dart';
import 'package:hitutor/Widget/ChooserBox.dart';
import 'package:hitutor/Widget/MyButton.dart';
import 'package:hitutor/Widget/MyDialog.dart';
import 'package:hitutor/Widget/MyPage.dart';

class SetInterest extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _SetInterest();
  }
}

class _SetInterest extends State<SetInterest> {
  List<dynamic> allInterests = [];
  List<dynamic> chooseInterests = [];

  chooseInterest(int i) {
    showWheelDialog2(context, "", allInterests, "interestType", (index) {
      var interest = allInterests[index];
      interest["priority"] = i;
      chooseInterests.removeWhere((element) => element["priority"] == i);
      chooseInterests.add(interest);
      setState(() {});
    });
  }

  @override
  void initState() {
    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
      getInterest();
    });
    super.initState();
  }

  getInterest() {
    HttpGo(context).get("/Profile/GetInterestByStudent?studentId=${Global.studentId}", {}, (data) {
      allInterests = data["interests"];
      for (var item in allInterests) {
        if (item["priority"] != null) {
          chooseInterests.add(item);
        }
      }
      setState(() {});
    }, (code, error) => showToast(error));
  }

  updateInterest() {
    var param = {"studentId": Global.studentId, "interests": chooseInterests};
    HttpGo(context).post("/Profile/UpdateInterestByStudent", param, (data) {
      PageUtil().back(context);
    }, (code, error) => showToast(error));
  }

  @override
  Widget build(BuildContext context) {
    dynamic interest1 = {};
    dynamic interest2 = {};
    dynamic interest3 = {};
    dynamic interest4 = {};
    dynamic interest5 = {};
    for (var item in chooseInterests) {
      if (item["priority"] == 1) {
        interest1 = item;
      } else if (item["priority"] == 2) {
        interest2 = item;
      } else if (item["priority"] == 3) {
        interest3 = item;
      } else if (item["priority"] == 4) {
        interest4 = item;
      } else if (item["priority"] == 5) {
        interest5 = item;
      }
    }
    return MyPage(
      "喜愛的主題",
      Container(
          padding: EdgeInsets.all(12),
          child: Column(children: [
            Row(children: [
              Text("1.　", style: TextStyle(fontSize: 14, color: MyColor.Orange, fontWeight: FontWeight.bold)),
              Expanded(
                  child: ChooserBox(interest1["interestType"] ?? "", onTap: () {
                chooseInterest(1);
              }))
            ]),
            Container(height: 12),
            Row(children: [
              Text("2.　", style: TextStyle(fontSize: 14, color: MyColor.Orange, fontWeight: FontWeight.bold)),
              Expanded(
                  child: ChooserBox(interest2["interestType"] ?? "", onTap: () {
                chooseInterest(2);
              }))
            ]),
            Container(height: 12),
            Row(children: [
              Text("3.　", style: TextStyle(fontSize: 14, color: MyColor.Orange, fontWeight: FontWeight.bold)),
              Expanded(
                  child: ChooserBox(interest3["interestType"] ?? "", onTap: () {
                chooseInterest(3);
              }))
            ]),
            Container(height: 12),
            Row(children: [
              Text("4.　", style: TextStyle(fontSize: 14, color: MyColor.Orange, fontWeight: FontWeight.bold)),
              Expanded(
                  child: ChooserBox(interest4["interestType"] ?? "", onTap: () {
                chooseInterest(4);
              }))
            ]),
            Container(height: 12),
            Row(children: [
              Text("5.　", style: TextStyle(fontSize: 14, color: MyColor.Orange, fontWeight: FontWeight.bold)),
              Expanded(
                  child: ChooserBox(interest5["interestType"] ?? "", onTap: () {
                chooseInterest(5);
              }))
            ]),
          ])),
      rightWidgets: [TextBtn("更新　", style: TextStyle(color: MyColor.BlueTxt), onTap: updateInterest)],
    );
  }
}
